package cn.virens.library.uitls;

import android.util.Log;

import org.xutils.x;

/**
 * 日志工具
 * Created by loioi on 2016/11/22.
 */
public class LogUtil {
    private static boolean USE_SYSOUT = false;
    private static String STRING_FORMAT = "loioi:%s.%s(L:%s)";

    public static void e(String msg) {
        if (x.isDebug()) println(Log.ERROR, generateTag(), msg);
    }

    public static void e(String format, Object... args) {
        LogUtil.e(String.format(format, args));
    }

    public static void e(Throwable ex) {
        if (x.isDebug()) println(Log.ERROR, generateTag(), "error->" + ex.getMessage());
    }

    public static void d(String msg) {
        if (x.isDebug()) println(Log.DEBUG, generateTag(), msg);
    }

    public static void d(String format, Object... args) {
        LogUtil.d(String.format(format, args));
    }

    public static void d(Throwable ex) {
        if (x.isDebug()) println(Log.DEBUG, generateTag(), "error->" + ex.getMessage());
    }

    private static String generateTag() {
        StackTraceElement trace = new Throwable().getStackTrace()[2];

        String callerClazzName = trace.getClassName();

        String clazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        String methodName = trace.getMethodName();
        int lineNumber = trace.getLineNumber();

        return String.format(STRING_FORMAT, clazzName, methodName, lineNumber);
    }

    private static void println(int priority, String tag, String msg) {
        if (USE_SYSOUT) {
            StringBuilder builder = new StringBuilder();
            builder.append(tag);
            builder.append("/");
            builder.append(msg);

            System.out.println(builder.toString());
        } else {
            Log.println(priority, tag, msg);
        }
    }
}
